﻿using Sidvall.Collections;
using Sidvall.Data;
using System.Collections.Generic;
using System.Linq;

namespace Sidvall.IdentityManager.Data.ConfigModel.EntityManagers
{
	public partial class UserConfigurationManager<TEntity, TList>
	{
		#region GetFreeServerItemId

		public int GetFreeServerItemId(IUserConfiguration item)
		{
			int id, iMax;

			if (item == null)
				return 1;
			if (item.ServerItems == null)
				return 1;
			var ids = (from o in item.ServerItems
					   where o.ServerItemId != null
					   orderby o.ServerItemId
					   select o.ServerItemId.Value).ToList();
			iMax = ids.Count;
			for (int i = 0; i < iMax; i++)
			{
				id = ids[i];
				if (id != i + 1)
					return i + 1;
			}
			return iMax + 1;
		}

		#endregion
		#region GetValidConfiguration

		public TEntity GetValidConfiguration(TEntity item)
		{
			HashSet<int> ids;

			if (item == null)
			{
				item = (TEntity)this.EntityFacadeManager.ResourceFactory.CreateItem(Sidvall.IdentityManager.Data.ConfigModel.DomainObjects.UserConfiguration, null);
				item.StartPageId = ParameterManager.DefaultUserConfiguration.StartPageId;
				item.ShowNewsAtStartup = ParameterManager.DefaultUserConfiguration.ShowNewsAtStartup;
			}
			if (string.IsNullOrWhiteSpace(item.ResetPasswordEmailFromAddress))
				item.ResetPasswordEmailFromAddress = ParameterManager.DefaultUserConfiguration.ResetPasswordEmailFromAddress;
			if (string.IsNullOrWhiteSpace(item.ResetPasswordEmailSubject))
				item.ResetPasswordEmailSubject = ParameterManager.DefaultUserConfiguration.ResetPasswordEmailSubject;
			if (string.IsNullOrWhiteSpace(item.ResetPasswordEmailBody))
				item.ResetPasswordEmailBody = ParameterManager.DefaultUserConfiguration.ResetPasswordEmailBody;
			if (item.UserConfigurationId == null)
				item.UserConfigurationId = 1;
			if (item.ServerItems == null)
				item.SetServerItems((IDataRowProvider<IServerItem>)this.EntityFacadeManager.ResourceFactory.CreateItems(Sidvall.IdentityManager.Data.ConfigModel.DomainObjects.ServerItem));
			if (!item.ServerItems.Any())
			{
				var serverItem = (IServerItem)this.EntityFacadeManager.ResourceFactory.CreateItem(Sidvall.IdentityManager.Data.ConfigModel.DomainObjects.ServerItem, null);
				serverItem.ServerItemId = this.EntityFacadeManager.GenericEntityFacade.UserConfigurationManager.GetFreeServerItemId(item);
				item.ServerItems.Add(serverItem);
				item.DefaultServerItemId = serverItem.ServerItemId.Value;
				ConnectionTypeManager.InitializeServer(serverItem);
			}
			ids = new HashSet<int>();
			foreach (var serverItem in item.ServerItems)
			{
				if (serverItem.ServerItemId != null)
				{
					if (!ids.Contains(serverItem.ServerItemId.Value))
						ids.Add(serverItem.ServerItemId.Value);
					else
						serverItem.ServerItemId = null;
				}
			}
			foreach (var serverItem in item.ServerItems)
			{
				if (serverItem.ServerItemId == null)
					serverItem.ServerItemId = this.EntityFacadeManager.GenericEntityFacade.UserConfigurationManager.GetFreeServerItemId(item);
				if (serverItem.Plugins == null)
					serverItem.SetPlugins((IDataRowProvider<IPlugin>)this.EntityFacadeManager.ResourceFactory.CreateItems(Sidvall.IdentityManager.Data.ConfigModel.DomainObjects.Plugin));
				ids = new HashSet<int>();
				foreach (var plugin in serverItem.Plugins)
				{
					if (plugin.PluginId != null)
					{
						if (!ids.Contains(plugin.PluginId.Value))
							ids.Add(plugin.PluginId.Value);
						else
							plugin.PluginId = null;
					}
				}
				foreach (var plugin in serverItem.Plugins)
				{
					if (plugin.PluginId == null)
						plugin.PluginId = this.EntityFacadeManager.GenericEntityFacade.ServerItemManager.GetFreePluginId(serverItem);
				}
			}
			return item;
		}

		#endregion
	}
}